<!DOCTYPE html>
[#assign ctx=springMacroRequestContext.contextPath/]
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>售出窗口</title>
    <link rel="stylesheet" href="${ctx!}/statics/layui/css/layui.css" media="all">
    <script type="text/javascript" src="${ctx}/statics/layui/layui.js"></script>
    <script src="${ctx}/statics/js/jquery-3.6.0.js"></script>
    <script src="${ctx}/statics/js/xadmin.js"></script>
</head>
<body>
<form class="layui-form">
    <div class="layui-form-item" style="margin-top:10px;margin-left: 10px">
        <div class="layui-inline">
            <label class="layui-form-label" style="width: 150px">请输入您要卖出的份数:</label>
            <div class="layui-input-inline">
                <input  type="number" value=0 oninput="limit(this)" id="amountToSell" name="amountToSell" class="layui-input">
            </div>
        </div>
    </div>
    <div class="layui-form-item layui-layout-right" style="margin-top:10px;margin-right: 10px">
        <div class="layui-input-block">
            <button type="submit"  class="layui-btn" lay-submit lay-filter="form1">卖出</button>
        </div>
    </div>
</form>
<script>

    //限制可出售份数不大于最大份数
    //去除数字前方的0
    //让字符串为空时自动转换为0
    function limit(obj){
        if(obj.value==""||obj.value==null){
            $('#amountToSell')[0].value = 0;
        }
        else{
            $('#amountToSell')[0].value = obj.value.replace(/\b(0+)/gi,"")
        }
        if(obj.value>${maxAmount})
            $('#amountToSell')[0].value = ${maxAmount};
    }

    layui.use(['layer','form'],function (){
        var form=layui.form;
        var layer = layui.layer;

        form.on('submit(form1)',function (data){
            var amountToSell = data.field.amountToSell;
            $.post("/trade/sell", {amountToSell: amountToSell,code:'${code}'}, res=>{
                switch (res.code) {
                    case 200:
                        layer.msg(
                            res.msg,
                            // {
                            //     icon:1
                            // },
                            {
                                icon:1,
                                time:2000,
                            },
                            function(){
                                var iframe_index = parent.layer.getFrameIndex(window.name);
                                parent.layer.close(iframe_index);
                                window.parent.location.reload();
                            }
                        )
                        break;
                    case 500:
                        layer.msg(res.msg, {icon:2});
                        break;
                }
            });
            return false;
        })
    })
</script>
</body>
</html>